package com.lp.yupao.controller;

import com.lp.yupao.common.BaseResponse;
import com.lp.yupao.common.ErrorCode;
import com.lp.yupao.exception.BusinessException;
import com.lp.yupao.model.domain.Team;
import com.lp.yupao.model.domain.request.TeamJoinRequest;
import com.lp.yupao.model.domain.request.TeamSearchRequest;
import com.lp.yupao.model.domain.request.TeamUpdateRequest;
import com.lp.yupao.model.dto.TeamQuery;
import com.lp.yupao.model.vo.TeamVO;
import com.lp.yupao.service.TeamService;
import com.lp.yupao.utils.ResultUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/team")
@CrossOrigin(originPatterns = "*", allowCredentials = "true")
public class TeamController {
    @Resource
    private TeamService teamService;

    /**
     * 创建队伍
     *
     * @param teamQuery 队伍数据
     * @param request 请求
     * @return 队伍id
     */
    @PostMapping("/create")
    public BaseResponse<Long> createTeam(@RequestBody TeamQuery teamQuery, HttpServletRequest request){
        if (teamQuery == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        Team team = new Team();
        BeanUtils.copyProperties(teamQuery,team);
        long result = teamService.createTeam(team, request);
        return ResultUtils.success(result);
    }

    /**
     * 更新队伍信息
     *
     * @param teamRequest 队伍信息
     * @param request 请求
     * @return 更新后的队伍信息
     */
    @PostMapping("/update")
    public BaseResponse<Long> updateTeam(@RequestBody TeamUpdateRequest teamRequest, HttpServletRequest request){
        if (teamRequest == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        long updateResult = teamService.updateTeam(teamRequest, request);
        return ResultUtils.success(updateResult);
    }

    /**
     * 根据条件查询队伍数据(支持分页)
     *
     * @param teamSearchRequest 查询条件
     * @param request 请求
     * @return 队伍数据列表
     */
    @GetMapping("/search/list")
    public BaseResponse<List<TeamVO>> searchTeamList(TeamSearchRequest teamSearchRequest, HttpServletRequest request){
        if (teamSearchRequest == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
       List<TeamVO> teamList = teamService.searchTeamList(teamSearchRequest,request);
       return ResultUtils.success(teamList);
    }

    /**
     * 加入队伍
     *
     * @param teamJoinRequest 请求参数（队伍id，密码）
     * @param request 请求
     * @return 是否加入成功
     */
    @PostMapping("/joinTeam")
    public BaseResponse<Integer> joinTeam(@RequestBody TeamJoinRequest teamJoinRequest, HttpServletRequest request) {
        if (teamJoinRequest == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        int result = teamService.insertTeam(teamJoinRequest,request);
        return ResultUtils.success(result);
    }

    /**
     * 退出队伍
     *
     * @param teamId 队伍id
     * @param request 请求
     * @return 是否退出成功
     */
    @GetMapping("/quitTeam")
    public BaseResponse<Integer> quitTeam(@RequestParam("id")long teamId,HttpServletRequest request ){
        if (teamId <= 0){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        int result = teamService.quitTeam(teamId,request);
        return ResultUtils.success(result);
    }

    /**
     * 删除队伍
     *
     * @param teamId 队伍id
     * @param request 请求
     * @return 是否删除成功
     */
    @GetMapping("/delete")
    public BaseResponse<Long> deleteTeamById(@RequestParam("id") long teamId,HttpServletRequest request){
        if (teamId <= 0){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        long result = teamService.deleteTeamById(teamId,request);
        return ResultUtils.success(result);
    }

    /**
     * 通过队伍id查队伍信息
     *
     * @param teamId 队伍id
     * @param request 请求
     * @return 队伍信息
     */
    @GetMapping("/get")
    public BaseResponse<TeamVO> getTeamById(@RequestParam("id") long teamId,HttpServletRequest request ){
        if (teamId <= 0){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        Team team = teamService.getTeamById(teamId,request);
        TeamVO teamVO = new TeamVO();
        BeanUtils.copyProperties(team,teamVO);
        return ResultUtils.success(teamVO);
    }

    /**
     * 查询我创建的队伍
     *
     * @param searchText 关键字
     * @param request 请求
     * @return 队伍列表
     */
    @GetMapping("/list/my/create")
    public BaseResponse<List<TeamVO>> getMyCreateTeam(String searchText,HttpServletRequest request){
        if (searchText == null){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        List<TeamVO> myTeamList =teamService.getMyCreateTeam(searchText,request);
        return ResultUtils.success(myTeamList);
    }
    /**
     * 查询我创建的队伍
     *
     * @param searchText 关键字
     * @param request 请求
     * @return 队伍列表
     */
    @GetMapping("/list/my/join")
    public BaseResponse<List<TeamVO>> getMyJoinTeam(String searchText,HttpServletRequest request){
        if (searchText == null){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        List<TeamVO> myTeamList =teamService.getMyJoinTeam(searchText,request);
        return ResultUtils.success(myTeamList);
    }
}
